package com.hugh.commom.serializer.impl;

import com.alibaba.fastjson.JSON;
import com.hugh.commom.RpcRequest;
import com.hugh.commom.serializer.Serializer;


import java.io.IOException;

/**
 * @Author: Hugh
 * @Date: 2020/6/14
 */
public abstract class JSONSerializer implements Serializer {


    @Override
    public byte[] serialize(Object object) throws IOException {
        return JSON.toJSONString(object).getBytes();
    }

    @Override
    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws IOException {
        return JSON.parseObject(bytes, clazz);
    }

    public static void main(String[] args) {
        Object[] parameters = {"this is a client message"};
        Class<?>[] parameterTypes = {String.class};
        RpcRequest rpcRequest = RpcRequest.builder()
                .requestId(1L)
                .className("com.hugh.service.UserService")
                .methodName("sayHello")
                .parameters(parameters)
                .parameterTypes(parameterTypes)
                .build();
        byte[] bytes = JSON.toJSONString(rpcRequest).getBytes();
        Object o = JSON.parseObject(bytes, RpcRequest.class);
        System.out.println(o);

    }

}
